<template>
  <div class="overflow-auto flex flex-col items-center">
    <m-svg-icon
      v-if="isMobileTerminal"
      name="close"
      class="w-3 h-3 p-0.5 m-1 ml-auto"
      fillClass="fill-zinc-900 dark:fill-zinc-200"
      @click="close"
    ></m-svg-icon>
    <img :src="blob" ref="imageTarget" class="w-[80%]" />
    <m-button
      class="mt-4 w-[80%] xl:w-1/2"
      @click="onConfirmClick"
      :loading="loading"
      >确定</m-button
    >
  </div>
</template>

<script>
const EMITS_CLOSE = 'close'
</script>

<script setup>
import { ref, onMounted } from 'vue'
import { isMobileTerminal } from '@/utils/flexible.js'
import Cropper from 'cropperjs'
import 'cropperjs/dist/cropper.css'
import { getOSSclient } from '@/utils/sts'
import { message } from '@/libs'
import { useStore } from 'vuex'
import { putProfile } from '@/api/sys.js'

const store = useStore()

const emits = defineEmits(['EMITS_CLOSE'])
defineProps({
  blob: {
    type: String,
    required: true
  }
})

/**
 * 关闭按钮点击事件
 */
const close = () => {
  emits(EMITS_CLOSE)
}

/**
 * 确定按钮点击
 */
const loading = ref(false)
const onConfirmClick = () => {
  // 开启 loading
  loading.value = true
  // 获取裁剪后的图片
  cropper.getCroppedCanvas().toBlob((blob) => {
    // 裁剪后的 blob 地址
    pubObjectToOSS(blob)
  })
}

/**
 * 进行 oss上传
 */
let ossClient
const pubObjectToOSS = async (file) => {
  if (!ossClient) {
    ossClient = await getOSSclient()
  }
  try {
    const fileTypeArr = file.type.split('/')
    const fileName = `${store.getters.userInfo.username}/${Date.now()}.${
      fileTypeArr[fileTypeArr.length - 1]
    }`
    // 1.存放的路径 包含名称
    // 2. file
    const res = await ossClient.put(`images/${fileName}`, file)
    onChangeProfile(res.url)
  } catch (e) {
    message('error', e)
  }
}

/**
 * 上传新头像到服务器
 */
const onChangeProfile = async (avatar) => {
  // 更新本地数据
  store.commit('user/setUserInfo', {
    ...store.getters.userInfo,
    avatar
  })
  // 更新服务器
  await putProfile(store.getters.userInfo)
  message('success', '用户头像修改成功')
  loading.value = false
  close()
}

// 移动端配置对象
const mobileOptions = {
  // 将裁剪框限制在画布的大小
  viewMode: 1,
  // 移动画布，裁剪框不动
  dragMode: 'move',
  // 裁剪框固定纵横比：1:1
  aspectRatio: 1,
  // 裁剪框不可移动
  cropBoxMovable: false,
  // 不可调整裁剪框大小
  cropBoxResizable: false
}

// PC 端配置对象
const pcOptions = {
  // 裁剪框固定纵横比：1:1
  aspectRatio: 1
}

/**
 * 图片裁剪
 */
const imageTarget = ref(null)
let cropper = null
onMounted(() => {
  /**
   * 接收两个参数：
   * 1. 需要裁剪的图片 DOM
   * 2. options 配置对象
   */
  cropper = new Cropper(
    imageTarget.value,
    isMobileTerminal.value ? mobileOptions : pcOptions
  )
})
</script>

<style lang="scss" scoped></style>
